package longcaisuyun.longcai.com.yiliubabalongcaisuyun;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.zcx.helper.activity.AppPictureActivity;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilBitmap;

import longcaisuyun.longcai.com.mystatic.publicUrl;
import longcaisuyun.longcai.com.net.PostOwnData;
import longcaisuyun.longcai.com.net.PostPic;

//                            _ooOoo_
//                           o8888888o
//                           88" . "88
//                           (| -_- |)
//                            O\ = /O
//                        ____/`---'\____
//                      .   ' \\| |// `.
//                       / \\||| : |||// \
//                     / _||||| -:- |||||- \
//                       | | \\\ - /// | |
//                     | \_| ''\---/'' | |
//                      \ .-\__ `-` ___/-. /
//                   ___`. .' /--.--\ `. . __
//                ."" '< `.___\_<|>_/___.' >'"".
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |
//                 \ \ `-. \_ __\ /__ _/ .-` / /
//         ======`-.____`-.___\_____/___.-`____.-'======
//                            `=---='
//
//         .............................................
//         佛祖保佑             永无BUG
//         佛曰:
//                  写字楼里写字间，写字间里程序员；
//                  程序人员写程序，又拿程序换酒钱。
//                  酒醒只在网上坐，酒醉还来网下眠；
//                  酒醉酒醒日复日，网上网下年复年。
//                  但愿老死电脑间，不愿鞠躬老板前；
//                  奔驰宝马贵者趣，公交自行程序员。
//                  别人笑我忒疯癫，我笑自己命太贱；
//                  不见满街漂亮妹，哪个归得程序员？
/************************************************************/
//

/************************************************************/

public class OwnDataActivity extends AppPictureActivity implements View.OnClickListener {

    TextView t_mima;
    LinearLayout lin_fh;
    ImageView iv_icon;
    private Dialog dialog;
    TextView tv_name, t_number, t_data, t_shenfen, t_yingang;
    String pic = "";
    private DisplayImageOptions options = new DisplayImageOptions.Builder()
            .showImageOnLoading(R.mipmap.iconmain) // 设置图片下载期间显示的图片
            .showImageForEmptyUri(R.mipmap.iconmain) // 设置图片Uri为空或是错误的时候显示的图片
            .showImageOnFail(R.mipmap.iconmain) // 设置图片加载或解码过程中发生错误显示的图片
            .cacheInMemory(false) // 设置下载的图片是否缓存在内存中
            .cacheOnDisk(true) // 设置下载的图片是否缓存在SD卡中
            .displayer(new FadeInBitmapDisplayer(100)).build(); // 构建完成
    private ImageLoader myimageloader = ImageLoader.getInstance();

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_own_data);
        View view = getWindow().getDecorView();
        // 适配工具将视图重新排布
        MyApplication.ScaleScreenHelper.loadView((ViewGroup) view);
        ViewInit();
        HttpPost();
    }

    private void HttpPost() {
        MyApplication.myviewutil.startProgressDialog(OwnDataActivity.this);
        new PostOwnData(MyApplication.myPreferences.readUid(), new AsyCallBack<PostOwnData.Info>() {
            @Override
            public void onSuccess(String toast, int type, PostOwnData.Info info) throws Exception {
                super.onSuccess(toast, type, info);
                if (info.message.equals("0")) {
                    Toast.makeText(OwnDataActivity.this, "获取数据失败，请稍后尝试", Toast.LENGTH_SHORT).show();
                } else {

                    tv_name.setText(info.username);
                    t_number.setText(info.mobile);
                    t_data.setText(info.models);
                    t_shenfen.setText(info.idnumber);
                    t_yingang.setText(info.banknumber);
                    myimageloader.displayImage(publicUrl.headurl + info.picurl,
                            iv_icon, options);
                }
                MyApplication.myviewutil.stopProgressDialog();
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                MyApplication.myviewutil.stopProgressDialog();
            }
        }).execute(OwnDataActivity.this);
    }

    private void ViewInit() {
        LinearLayout fanhui_lin = (LinearLayout) findViewById(R.id.fanhui_lin);
        fanhui_lin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        t_mima = (TextView) findViewById(R.id.t_mima);
        t_mima.setOnClickListener(this);

        iv_icon = (ImageView) findViewById(R.id.iv_icon);
        iv_icon.setOnClickListener(this);
        tv_name = (TextView) findViewById(R.id.tv_name);
        t_number = (TextView) findViewById(R.id.t_number);
        t_data = (TextView) findViewById(R.id.t_data);
        t_shenfen = (TextView) findViewById(R.id.t_shenfen);
        t_yingang = (TextView) findViewById(R.id.t_yingang);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.t_mima:
                startActivity(new Intent(OwnDataActivity.this, MimaActivity.class));
                break;
            case R.id.lin_fh:
                OwnDataActivity.this.finish();
                break;
            case R.id.iv_icon:
                dialg();
                break;
        }
    }

    @Override
    protected String getCameraAbsolutePath() {
        return Environment.getExternalStorageDirectory() + "/168suyun";
    }

    @Override
    protected void resultPhotoPath(ImageView imageView, String s) {

        try {
            pic = UtilBitmap.toBase64ByPath(s, 720, 1280, 50);
            HttpPostPic(pic, s);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void dialg() {
        LayoutInflater inflater = LayoutInflater.from(OwnDataActivity.this);
        View layout =  inflater.inflate(
                R.layout.item_photo_icon, null);

        dialog = new Dialog(OwnDataActivity.this,R.style.MyDialog);
        Window window = dialog.getWindow();
        window.setGravity(Gravity.BOTTOM);
        window.setWindowAnimations(R.style.mypopwindow_anim_style);
        dialog.show();

        WindowManager windowManager = OwnDataActivity.this.getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
        lp.width = (int) (display.getWidth()); // 设置宽度
        dialog.getWindow().setAttributes(lp);

        MyApplication.ScaleScreenHelper.loadView((ViewGroup) layout);
        dialog.setContentView(layout);

        TextView t_carame = (TextView) layout.findViewById(R.id.t_camera);
        TextView t_xiangce = (TextView) layout.findViewById(R.id.t_xiangce);
        TextView t_cancle = (TextView) layout.findViewById(R.id.t_cancle);
        t_carame.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OwnDataActivity.this.startCamera(iv_icon);
                dialog.dismiss();
            }
        });
        t_xiangce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OwnDataActivity.this.startAlbum(iv_icon);
                dialog.dismiss();
            }
        });
        t_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
    }

    protected Crop getCrop() {


        return new Crop().setCrop(true);

    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    public void HttpPostPic(String pic, final String s) {
        MyApplication.myviewutil.startProgressDialog(OwnDataActivity.this);
        new PostPic(MyApplication.myPreferences.readUid(), pic, new AsyCallBack<PostPic.Info>() {
            @Override
            public void onSuccess(String toast, int type, PostPic.Info info) throws Exception {
                super.onSuccess(toast, type, info);
                if (info.mesaage.equals("1")) {
                    Toast.makeText(OwnDataActivity.this, "修改头像成功", Toast.LENGTH_SHORT).show();
                    iv_icon.setImageBitmap(BitmapFactory.decodeFile(s));
                } else {
                    Toast.makeText(OwnDataActivity.this, "修改头像失败，请稍后尝试", Toast.LENGTH_SHORT).show();
                }
                MyApplication.myviewutil.stopProgressDialog();
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                MyApplication.myviewutil.stopProgressDialog();
                Toast.makeText(OwnDataActivity.this, "修改头像失败，请查看网络设置后重新尝试", Toast.LENGTH_SHORT).show();
            }
        }).execute(OwnDataActivity.this);
    }
}
